#include "led.h"

static int led_probe(struct platform_device *pdev);
static int led_remove(struct platform_device *pdev);

static int led_open(struct inode *inode, struct file *file);
static ssize_t led_read(struct file *file, char __user *buf, size_t len, loff_t *pos);
static ssize_t led_write(struct file *file, const char __user *buf, size_t len, loff_t *pos);

//设备号
static dev_t led_num;
//cdev对象
static struct cdev led_cdev;
//class对象
static struct class *led_class;
//操作函数
static struct file_operations led_ops = {
	.owner = THIS_MODULE,
	.open = led_open,
	.read = led_read,
	.write = led_write,
};

/* 匹配列表，用于设备树和平台驱动匹配 */
static const struct of_device_id led_of_match[] = {
	{.compatible = "atk,led"},
	{ /* Sentinel */ }
};
//平台驱动
static struct platform_driver led_drv = {
	.driver = {
		.name = "led",
		.owner = THIS_MODULE,
		.pm = NULL,
		.of_match_table = led_of_match,
	},
	.probe = led_probe,
	.remove = led_remove,
};

//led句柄列表
static struct list_head led_list = LIST_HEAD_INIT(led_list);

//根据ID查找设备句柄
static struct led_handle *find_led_handle(uint32_t id)
{
	struct led_handle *pos;
	struct led_handle *n;
	struct led_handle *led_handle;

	led_handle = NULL;
	list_for_each_entry_safe(pos, n, &led_list, node)
	{
		if(pos->id == id)
		{
			led_handle = pos;
			break;
		}
	}

	return led_handle;
}

//打开LED设备
static int led_open(struct inode *inode, struct file *file)
{
	uint32_t id;
	struct led_handle *led_handle;

	//提取设备ID
	id = MINOR(inode->i_rdev);

	//查找设备句柄
	led_handle = find_led_handle(id);
	if(!led_handle)
	{
		printk("find led handle failed\r\n");
		return -EINVAL;
	}

	//设置文件私有数据
	file->private_data = (void*)led_handle;
	printk("%s\n", led_handle->labe);

	return 0;
}

//读取LED状态
static ssize_t led_read(struct file *file, char __user *buf, size_t len, loff_t *pos)
{
	char kernal_data;
	struct led_handle *led_handle = file->private_data;

	if(led_handle->state == 0)
		kernal_data = '0';
	else if(led_handle->state == 1)
		kernal_data = '1';
	else
		return -EINVAL;

	//将数据从内核空间拷贝到用户空间
	if(copy_to_user(buf, &kernal_data, 1))
		return -EFAULT;

	return 1;
}

//控制LED状态
static ssize_t led_write(struct file *file, const char __user *buf, size_t len, loff_t *pos)
{
	char user_data;
	struct led_handle *led_handle = file->private_data;

	//将数据从用户框架拷贝到内核框架
	if(copy_from_user(&user_data, buf, 1))
		return -EFAULT;

	if(user_data == '0')
	{
		led_handle->state = 0;
		gpiod_set_value(led_handle->led_gpio, 1);
	}
	else if(user_data == '1')
	{
		led_handle->state = 1;
		gpiod_set_value(led_handle->led_gpio, 0);
	}
	else
		return -EINVAL;

	return len;
}

//设备和驱动匹配成功执行
static int led_probe(struct platform_device *pdev)
{
	int result;
	uint32_t id;
	struct device *device;
	struct led_handle *led_handle;

	printk("%s\r\n", __FUNCTION__);

	//按从小到大顺序生成ID
	for(id=0; (find_led_handle(id)) && (id<LED_NUMBER); id++)
	{
		;
	}
	//ID必须小于注册的最大ID号
	if(id >= LED_NUMBER)
	{
		printk("dev id invalid\r\n");
		return -EINVAL;
	}

	//设置平台设备ID
	pdev->id = id;

	//分配设备句柄
	led_handle = devm_kzalloc(&pdev->dev, sizeof(struct led_handle), GFP_KERNEL);
	if(!led_handle)
	{
		printk("alloc memory failed\r\n");
		return -ENOMEM;
	}

	//绑定ID
	led_handle->id = id;

	//获取labe
	result = of_property_read_string(pdev->dev.of_node, "labe", &led_handle->labe);
	if(result < 0)
	{
		printk("get labe failed\r\n");
		return result;
	}

	//获取GPIO，并设置为输出低电平
	led_handle->led_gpio = devm_gpiod_get(&pdev->dev, "led", GPIOD_OUT_LOW);
	if(IS_ERR(led_handle->led_gpio))
	{
		printk("get gpio failed\r\n");
		return PTR_ERR(led_handle->led_gpio);
	}
	led_handle->state = 0;

	//设置平台设备的驱动私有数据
	pdev->dev.driver_data = (void*)led_handle;

	//加入设备句柄链表
	list_add(&led_handle->node, &led_list);

	printk("device number = %d, device name = %s\r\n", led_num+led_handle->id, led_handle->labe);
	//创建设备文件，将ID作为此设备的次设备号
	device = device_create(led_class, NULL, led_num+led_handle->id, NULL, led_handle->labe);
	if(IS_ERR(device))
	{
		list_del(&led_handle->node);
		printk("device create failed");
		return PTR_ERR(device);
	}

	return 0;
}

//设备或驱动卸载时执行
static int led_remove(struct platform_device *pdev)
{
	struct led_handle *led_handle;

	printk("%s\r\n", __FUNCTION__);

	//提取平台设备的驱动私有数据
	led_handle = (struct led_handle*)pdev->dev.driver_data;

	//删除设备文件
	device_destroy(led_class, led_num+led_handle->id);

	//从设备句柄链表中删除
	list_del(&led_handle->node);

	return 0;
}

static int __init led_drv_init(void)
{
	int result;

	printk("%s\r\n", __FUNCTION__);

	//根据次设备号起始值动态注册字符设备号
	result = alloc_chrdev_region(&led_num, 0, LED_NUMBER, "atk,led");
	if(result < 0)
	{
		printk("alloc chrdev failed\r\n");
		return result;
	}
	printk("first device major %d, minor %d\r\n", MAJOR(led_num), MINOR(led_num));

	//初始化CDEV对象
	cdev_init(&led_cdev, &led_ops);
	//向系统添加CDEV对象
	result = cdev_add(&led_cdev, led_num, LED_NUMBER);
	if(result < 0)
	{
		unregister_chrdev_region(led_num, LED_NUMBER);
		printk("add cdev failed\r\n");
		return result;
	}

	//创建class对象
	led_class = class_create(THIS_MODULE, "led,class");
	if(IS_ERR(led_class))
	{
		cdev_del(&led_cdev);
		unregister_chrdev_region(led_num, LED_NUMBER);
		printk("class create failed");
		return PTR_ERR(led_class);
	}

	//注册平台驱动
	result = platform_driver_register(&led_drv);
	if(result < 0)
	{
		class_destroy(led_class);
		cdev_del(&led_cdev);
		unregister_chrdev_region(led_num, LED_NUMBER);
		printk("add cdev failed\r\n");
		return result;
	}

	return 0;
}

static void __exit led_drv_exit(void)
{
	printk("%s\r\n", __FUNCTION__);

	//注销平台驱动
	platform_driver_unregister(&led_drv);
	//销毁class对象
	class_destroy(led_class);
	//从系统删除CDEV对象
	cdev_del(&led_cdev);
	//注销字符设备号
	unregister_chrdev_region(led_num, LED_NUMBER);
}

module_init(led_drv_init);
module_exit(led_drv_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("LF");
MODULE_DESCRIPTION("led_dev");
